package com.example.administrator.helloworld;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


public class MainActivity extends Activity {
    private CheckBox sleep;
    private CheckBox eat;
    private CheckBox dota;
    private TextView text;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private ToggleButton toggleButton;
    private Spinner mySpinner;
    private SimpleAdapter sa;
    private List<Map<String,Object>> dataList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*this.requestWindowFeature(Window.FEATURE_NO_TITLE);*/
        //supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);//关联layout
        //this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        dataList = new ArrayList<Map<String,Object>>();
        getData();
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        toggleButton = (ToggleButton) findViewById(R.id.tb);
        text = (TextView)findViewById(R.id.haha);
        eat = (CheckBox)findViewById(R.id.eat);
        sleep = (CheckBox)findViewById(R.id.sleep);
        dota = (CheckBox)findViewById(R.id.dota);
        mySpinner = (Spinner) findViewById(R.id.mySpinner);
        sa = new SimpleAdapter(this,dataList,R.layout.item,new String[] {"image","text"},new int[]{R.id.iv,R.id.tv});
        sa.setDropDownViewResource(R.layout.item);
        mySpinner.setAdapter(sa);

        Bl1 bl1 = new Bl1();
        Bl2 bl2 = new Bl2();
        OnBoxClickListener boxClickListener = new OnBoxClickListener();
        toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(text.getText()=="Hello world!"){
                    text.setText("日了狗了");
                    text.setBackgroundColor(Color.BLUE);
                }
                else{
                    text.setText("Hello world!");
                    text.setBackgroundColor(Color.GREEN);
                }
            }
        });

        btn1.setOnClickListener(bl1);
        btn2.setOnClickListener(bl2);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,CalActivity.class);
                startActivity(intent);
            }
        });
        eat.setOnClickListener(boxClickListener);
        sleep.setOnClickListener(boxClickListener);
        dota.setOnClickListener(boxClickListener);
        mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                text.setText("您选择的是" + dataList.get(position).get("text"));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                text.setText("您尚未选择");
            }
        });

        if(savedInstanceState!=null){
            String tempData = savedInstanceState.getString("data_key");
            Log.d("tag",tempData);
        }
    }

    private void getData() {
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("text","北京");
        map.put("image",R.drawable.ic_launcher);
        Map<String,Object> map2 = new HashMap<String, Object>();
        map2.put("text","上海");
        map2.put("image",R.drawable.ic_launcher);
        Map<String,Object> map3 = new HashMap<String, Object>();
        map3.put("text","广州");
        map3.put("image",R.drawable.ic_launcher);
        Map<String,Object> map4 = new HashMap<String, Object>();
        map4.put("text","深圳");
        map4.put("image",R.drawable.ic_launcher);
        Map<String,Object> map5 = new HashMap<String, Object>();
        map5.put("text","杭州");
        map5.put("image",R.drawable.ic_launcher);
        dataList.add(map);
        dataList.add(map2);
        dataList.add(map3);
        dataList.add(map4);
        dataList.add(map5);


    }

    @Override
    protected void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);
        String tempData = "something important";
        outState.putString("data_key",tempData);
    }
    //取消则没有呼出菜单按键
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    class OnBoxClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.eat:
                    Intent intent2A = new Intent(MainActivity.this,TestAdapterActivity.class);
                    startActivity(intent2A);
                    break;
                case R.id.sleep:
                    Intent intent2L = new Intent(MainActivity.this,TestLeanCloud.class);
                    startActivity(intent2L);
                    break;
                case R.id.dota:
                    break;
                default:break;
            }
            //Toast.makeText(MainActivity.this,"CheckBox Changed",Toast.LENGTH_SHORT).show();
        }
    }

    class Bl1 implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            if(text.getText()=="Hello world!"){
                text.setText("日了狗了");
                text.setBackgroundColor(Color.BLUE);
            }
            else{
                text.setText("Hello world!");
                text.setBackgroundColor(Color.GREEN);
            }
            String data="Hello Modo";
            Intent intent = new Intent(MainActivity.this,FruitActivity.class);
            intent.putExtra("extra_data", data);
            startActivity(intent);
        }
    }
    class Bl2 implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this,ChatActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        switch(item.getItemId()){
            case R.id.action_settings:
                Toast.makeText(this,"settings",Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_about:
                Toast.makeText(this,"about",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;

        //return super.onOptionsItemSelected(item);
    }
}
